<template>
  <div class="menu">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-button @click="$router.push({
          name:'menu-create'
        })">添加菜单</el-button>
      </div>
      <div class="text item">
        <el-table
          border
          :data="menuList"
          style="width: 100%">
          <el-table-column
            type="index"
            label="编号"
            align="center"
          >
          </el-table-column>
          <el-table-column
            prop="name"
           align="center"
            label="菜单名称"
          >
          </el-table-column>
          <el-table-column
            prop="level"
            align="center"
            label="菜单级数">
          </el-table-column>
          <el-table-column
            prop="icon"
            align="center"
            label="前端图标">
          </el-table-column>
          <el-table-column
            prop="orderNum"
            align="center"
            label="排序">
          </el-table-column>
          <el-table-column
            prop="orderNum"
            align="center"
            label="操作">
            <template slot-scope="scope">
              <el-button
                size="mini"
                @click="handleEdit(scope.row)">编辑</el-button>
              <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-card>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import { getAllMenus, deleteMenu } from '@/services/menu'
export default Vue.extend({
  name: 'menuName',
  data () {
    return {
      menuList: []
    }
  },
  created () {
    this.getMenuList()
  },
  methods: {
    async getMenuList () {
      const { data } = await getAllMenus()
      if (data.code === '000000') {
        this.menuList = data.data
      }
    },
    handleEdit (item: any) {
      this.$router.push({
        name: 'menu-editor',
        params: {
          id: item.id
        }
      })
      console.log('handleEdit')
    },
    handleDelete (item: any) {
      this.$confirm('确认删除吗？', '删除提示', {})
        .then(async () => {
          const { data } = await deleteMenu(item.id)
          if (data.code === '000000') {
            this.$message.success('删除成功')
            this.getMenuList()
          }
        })
        .catch(err => {
          console.log(err)
          this.$message.info('以取消删除')
        })
      console.log('handleDelete')
    }
  }
})
</script>
<style lang='scss' scoped>
</style>
